package com.wandian.texas.entity;

import com.wandian.texas.CalculateCard;

/**
 * 游戏最终计算钱，从这里取比较准确，因为玩家游戏中场游戏房间，user信息会改变
 * @author long
 * @date 2013-1-4 下午12:03:29
 * @qq 108232706
 * @description UserGameEntity
 */
public class UserGameEntity implements Comparable<UserGameEntity> {
	public UserInfoEntity user;
	public int seatId;
	public int isWin;
	/**
	 * 如1，那么0,1都下了注
	 */
	public int bonusIndex;
	public int lastAction;
	/**
	 * 游戏过程中未入边池的钱.为了入池准备
	 */
	public float seatChip;
	/**
	 * 游戏总下注，为了统计总下注需要
	 */
	public float totalBet;
	public float totalWin;
	public String[] cards = new String[2];
	//	public String card1;
	//	public String card2;
	/**
	 * 为了判断nextOperate
	 */
	public int step;
	/**
	 * 当前玩家在每边池的赢分情况
	 */
	public float[] bonusWin = new float[9];
	private final WinCardEntity maxHandcard = new WinCardEntity();
	private final GameRoomEntity game;

	public int getType() {
		return maxHandcard.getType();
	}

	public UserGameEntity(GameRoomEntity game) {
		this.game = game;
	}

	/**
	 * 游戏开始
	 * @param user
	 */
	public void start(UserInfoEntity user) {
		this.user = user;
		this.seatId = user.seatId;
	}

	/**
	 * 游戏结束的时候，需要清空
	 */
	public void clear() {
//		seatId = 0;
		isWin = 0;
		totalBet = 0;
		totalWin = 0;
		lastAction = 0;// LastAction.FOLD;
		bonusIndex = 0;
		seatChip = 0;
		step = 0;
		cards[0] = "-";
		cards[1] = "-";
		for (int i = 0; i < 9; i++) {
			bonusWin[i] = 0;
		}
		user = null;
		maxHandcard.clear();
	}

	public void clearCard() {
		cards[0] = "-";
		cards[1] = "-";
		maxHandcard.clear();
	}

	public float maxRaiseStake() {
		if (this.user == null)
			return 0;
		return this.seatChip + this.user.stake;
	}

	public int cardType() {
		return getMaxHandcard().getType();
	}

	public int compareTo(UserGameEntity o) {
		if (o instanceof UserGameEntity) {
			if (cardType() == o.cardType()) {
				return isMoreThan(o);
			} else {
				return cardType() - o.cardType();
			}
		}
		return -1;
	}

	public int isMoreThan(UserGameEntity user) {
		int result = cardType() - user.cardType();
		if (result != 0) {
			return result;
		}
		// 比较大小值
		switch (cardType()) {
		// 皇家同花顺 type:9 isWin:0 huase:4 arrCard:arr1
		// 长度:4[黑桃14][黑桃13][黑桃12][黑桃11]最大手牌:长度:6[黑桃14][黑桃13][黑桃12][黑桃11][黑桃10][黑桃1]
		case 9:
			result = 0;
			break;
		case 8:
			// 同花顺 type:8 isWin:0 huase:4 arrCard:arr1
			// 长度:5[黑桃13][黑桃12][黑桃11][黑桃10][黑桃9]最大手牌:长度:5[黑桃13][黑桃12][黑桃11][黑桃10][黑桃9]
			result = maxHandcard.getHandCardsValue(0) - user.getHandCardsValue(0);
			break;
		case 7:
			// 四条 type:7 isWin:0 huase:0 arrCard:arr1
			// 长度:4[方块6][梅花6][红桃6][黑桃6]最大手牌:长度:5[方块6][梅花6][红桃6][黑桃6][方块4]
			if (maxHandcard.getHandCardsValue(0) == 1 && user.getHandCardsValue(0) != 1) {
				return 1;
			} else if (maxHandcard.getHandCardsValue(0) != 1 && user.getHandCardsValue(0) == 1) {
				return -1;
			}
			if (maxHandcard.getHandCardsValue(4) == 1 && user.getHandCardsValue(4) != 1) {
				return 1;
			} else if (maxHandcard.getHandCardsValue(4) != 1 && user.getHandCardsValue(4) == 1) {
				return -1;
			}
			result = maxHandcard.getHandCardsValue(4) - user.getHandCardsValue(4);

			break;
		case 6:
			// 葫芦 type:6 isWin:0 huase:0 arrCard:arr1
			// 长度:3[方块8][梅花8][红桃8]arr2:长度:2[梅花11][红桃11]最大手牌:长度:5[梅花11][红桃11][方块8][梅花8][红桃8]
			if (maxHandcard.getHandCardsValue(0) == 1 && user.getHandCardsValue(0) != 1) {
				return 1;
			} else if (maxHandcard.getHandCardsValue(0) != 1 && user.getHandCardsValue(0) == 1) {
				return -1;
			}
			if (maxHandcard.getHandCardsValue(4) == 1 && user.getHandCardsValue(4) != 1) {
				return 1;
			} else if (maxHandcard.getHandCardsValue(4) != 1 && user.getHandCardsValue(4) == 1) {
				return -1;
			}
			result = maxHandcard.getHandCardsValue(4) - user.getHandCardsValue(4);
			break;
		case 5:
			// 同花 type:5 isWin:0 huase:3 arrCard:arr1
			// 长度:5[红桃13][红桃10][红桃7][红桃5][红桃3]最大手牌:长度:5[红桃13][红桃10][红桃7][红桃5][红桃3]
			for (int i = 0; i < 5; i++) {
				if (maxHandcard.getHandCardsValue(i) == 1 && user.getHandCardsValue(i) != 1) {
					return 1;
				} else if (maxHandcard.getHandCardsValue(i) != 1 && user.getHandCardsValue(i) == 1) {
					return -1;
				}
				result = maxHandcard.getHandCardsValue(i) - user.getHandCardsValue(i);
				if (result != 0) {
					break;
				}
			}
			break;
		case 4:
			// 顺子 type:4 isWin:0 huase:-1 arrCard:arr1
			// 长度:5[黑桃8][黑桃7][红桃6][梅花5][方块4]最大手牌:长度:5[黑桃8][黑桃7][红桃6][梅花5][方块4]
			result = maxHandcard.getHandCardsValue(0) - user.getHandCardsValue(0);
			break;
		case 3:
			// 三条 type:3 isWin:0 huase:0 arrCard:arr1
			// 长度:3[方块8][梅花8][红桃8]最大手牌:长度:5[黑桃11][黑桃10][方块8][梅花8][红桃8]
			if (maxHandcard.getHandCardsValue(0) == 1 && user.getHandCardsValue(0) != 1) {
				return 1;
			} else if (maxHandcard.getHandCardsValue(0) != 1 && user.getHandCardsValue(0) == 1) {
				return -1;
			}
			result = maxHandcard.getHandCardsValue(0) - user.getHandCardsValue(0);
			if (result == 0) {
				if (maxHandcard.getHandCardsValue(3) == 1 && user.getHandCardsValue(3) != 1) {
					return 1;
				} else if (maxHandcard.getHandCardsValue(3) != 1 && user.getHandCardsValue(3) == 1) {
					return -1;
				}
				result = maxHandcard.getHandCardsValue(3) - user.getHandCardsValue(3);
				if (result == 0) {
					result = maxHandcard.getHandCardsValue(4) - user.getHandCardsValue(4);
				}
			}
			break;
		case 2:
			// 两对 type:2 isWin:0 huase:0 arrCard:arr1
			// 长度:2[黑桃10][红桃10]arr2:长度:2[方块2][梅花2]最大手牌:长度:5[黑桃10][红桃10][黑桃7][方块2][梅花2]
			if (maxHandcard.getHandCardsValue(0) == 1 && user.getHandCardsValue(0) != 1) {
				return 1;
			} else if (maxHandcard.getHandCardsValue(0) != 1 && user.getHandCardsValue(0) == 1) {
				return -1;
			}
			result = maxHandcard.getHandCardsValue(0) - user.getHandCardsValue(0);
			if (result == 0) {
				if (maxHandcard.getHandCardsValue(2) == 1 && user.getHandCardsValue(2) != 1) {
					return 1;
				} else if (maxHandcard.getHandCardsValue(2) != 1 && user.getHandCardsValue(2) == 1) {
					return -1;
				}
				result = maxHandcard.getHandCardsValue(2) - user.getHandCardsValue(2);
				if (result == 0) {
					if (maxHandcard.getHandCardsValue(4) == 1 && user.getHandCardsValue(4) != 1) {
						return 1;
					} else if (maxHandcard.getHandCardsValue(4) != 1 && user.getHandCardsValue(4) == 1) {
						return -1;
					}
					result = maxHandcard.getHandCardsValue(4) - user.getHandCardsValue(4);
				}
			}
			break;
		case 1:
			// 一对 type:1 isWin:0 huase:0 arrCard:arr1
			// 长度:2[梅花8][红桃8]最大手牌:长度:5[方块11][梅花8][红桃8][黑桃7][红桃4]
			if (maxHandcard.getHandCardsValue(0) == 1 && user.getHandCardsValue(0) != 1) {
				return 1;
			} else if (maxHandcard.getHandCardsValue(0) != 1 && user.getHandCardsValue(0) == 1) {
				return -1;
			}
			result = maxHandcard.getHandCardsValue(0) - user.getHandCardsValue(0);
			if (result == 0) {
				if (maxHandcard.getHandCardsValue(2) == 1 && user.getHandCardsValue(2) != 1) {
					return 1;
				} else if (maxHandcard.getHandCardsValue(2) != 1 && user.getHandCardsValue(2) == 1) {
					return -1;
				}
				for (int i = 2; i < 5; i++) {
					result = maxHandcard.getHandCardsValue(i) - user.getHandCardsValue(i);
					if (result != 0) {
						break;
					}
				}
			}
			break;
		case 0:
			// 高牌 type:0 isWin:0 huase:0 arrCard:arr1
			// 长度:5[方块11][黑桃7][黑桃6][红桃5][方块2]最大手牌:长度:5[方块11][黑桃7][黑桃6][红桃5][方块2]
			if (maxHandcard.getHandCardsValue(0) == 1 && user.getHandCardsValue(0) != 1) {
				return 1;
			} else if (maxHandcard.getHandCardsValue(0) != 1 && user.getHandCardsValue(0) == 1) {
				return -1;
			}
			for (int i = 0; i < 5; i++) {
				result = maxHandcard.getHandCardsValue(i) - user.getHandCardsValue(i);
				if (result != 0) {
					break;
				}
			}
			break;
		default:
			break;
		}
		return result;
	}

	//	public int singleCardValue() {
	//		return getMaxHandcard().singleCardValue();
	//	}
	//
	//	public int maxCards1Value() {
	//		return getMaxHandcard().maxCards1Value();
	//	}
	//
	//	public int maxCards1Value(int index) {
	//		return getMaxHandcard().maxCards1Value(index);
	//	}
	//
	//	public int maxCards2Value() {
	//		return getMaxHandcard().maxCards2Value();
	//	}
	//
	//	public int maxHandCardsValue() {
	//		return getMaxHandcard().maxHandCardsValue();
	//	}

	public int getHandCardsValue(int index) {
		return getMaxHandcard().getHandCardsValue(index);
	}

	public String getCard(int index) {
		if (maxHandcard != null) {
			return getMaxHandcard().getCard(index);
		}
		return "";
	}

	public WinCardEntity getMaxHandcard() {
		return maxHandcard;
	}

	public String getMaxCards() {
		if (this.maxHandcard == null)
			return "";
		return this.maxHandcard.getMaxCards();
	}

	/**
	 * 计算坐位的牌
	 */
	public void calculateCard() {
		CalculateCard.countCardType(this.game, this.maxHandcard, game.PublicCard, this.cards);
	}

	//	public void setMaxHandcard(WinCardEntity maxHandcard) {
	//		this.maxHandcard = null;
	//		this.maxHandcard = maxHandcard;
	//		//		Log.debug(String.format("玩家%s,当前状态%s,坐位%s,最大牌型%s", this.nickName, GamePool.getLastAction(this.lastAction), this.seatId, this.maxHandcard.toString()));
	//	}

	public int step() {
		return this.step++;
	}
}
